/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React, { Fragment } from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import CodePush, { DownloadProgress } from 'react-native-code-push';
import CodePushManager, { StatusStr } from 'src/service/CodePushManager';
import Config from './config';

// const IndexView = () => {
//   const { percent, status } = this.props;
//   return (
//
//   );
// };
export default class App extends React.Component {
  constructor(props) {
    super(props);
    CodePushManager.syncSilently(CodePush.InstallMode.IMMEDIATE);
    CodePushManager.addListener(
      'App',
      percent => {
        this.setState({ percent });
      },
      status => {
        this.setState({ status });
      }
    );
    // CodePush.sync({
    //   installMode: CodePush.InstallMode.IMMEDIATE,
    // }, (status: CodePush.SyncStatus) => {
    //   this.setState({ status });
    // }, (progress: DownloadProgress) => {
    //   const percent = parseFloat(progress.receivedBytes)/parseFloat(progress.totalBytes);
    //   this.setState({ percent });
    // });
  }
  state = {};

  componentWillUnmount() {
    CodePushManager.removeListener('App');
  }

  render() {
    const { percent, status } = this.state;
    return (
      <Fragment>
        <StatusBar barStyle="dark-content" />
        <SafeAreaView>
          <ScrollView
            contentInsetAdjustmentBehavior="automatic"
            style={styles.scrollView}
          >
            <Header />
            <Text>{`Version ${Config.version} | ${
              StatusStr[status]
            }(${parseFloat(percent || 0).toFixed(2)}%)`}</Text>
            {global.HermesInternal == null ? null : (
              <View style={styles.engine}>
                <Text style={styles.footer}>Engine: Hermes</Text>
              </View>
            )}
            <View style={styles.body}>
              <View style={styles.sectionContainer}>
                <Text style={styles.sectionTitle}>Step One</Text>
                <Text style={styles.sectionDescription}>
                  Edit <Text style={styles.highlight}>App.js</Text> to change
                  this screen and then come back to see your edits.
                </Text>
              </View>
              <View style={styles.sectionContainer}>
                <Text style={styles.sectionTitle}>See Your Changes</Text>
                <Text style={styles.sectionDescription}>
                  <ReloadInstructions />
                </Text>
              </View>
              <View style={styles.sectionContainer}>
                <Text style={styles.sectionTitle}>Debug</Text>
                <Text style={styles.sectionDescription}>
                  <DebugInstructions />
                </Text>
              </View>
              <View style={styles.sectionContainer}>
                <Text style={styles.sectionTitle}>Learn More</Text>
                <Text style={styles.sectionDescription}>
                  Read the docs to discover what to do next:
                </Text>
              </View>
              <LearnMoreLinks />
            </View>
          </ScrollView>
        </SafeAreaView>
      </Fragment>
    );
  }
}

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: Colors.dark,
  },
  highlight: {
    fontWeight: '700'
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: '600',
    padding: 4,
    paddingRight: 12,
    textAlign: 'right'
  },
});
